﻿@using DH.Web.MVC
@using DH.Service.ViewModels.Dto
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.DependencyInjection;
@inject IHtmlLocalizer<SharedResources> SharedLocalizer

@inject IServiceCollection serviceCollection 

@{
    <a class="btn btn-default" onclick="$dh.reload()"><span class="glyphicon glyphicon-refresh"></span></a>

    var currentUser = this.ViewBag.CurrentUser;
    var currentViewId = (string)this.ViewBag.CurrentViewId;

    //route 格式controller/**action  二级路由不一定就是aciton 名称
    //所以此处从路由模板获取路由各级名称
    //var routeTemplate = ViewContext.ActionDescriptor.AttributeRouteInfo.Template;
    var routeTemplate = ViewContext.ActionDescriptor.AttributeRouteInfo.Template;
    var routeName = routeTemplate.Split('/');
    var controllerName = routeName[0];
    var actionName = routeName[1];


    //获取页面功能
    var viewFuncDtos = Html.GetViewButtons(serviceCollection, currentViewId);

    if (currentUser.IsDeveloper)
    {

        foreach (var vfd in viewFuncDtos)
        {
            var btnId = controllerName + "_" + actionName + "_";
            //按钮id
            btnId += vfd.ButtonCode;

            if (vfd.ButtonCode == "btnAdd")
            {
                <button class="btn btn-default" id="@btnId" v-on:click="add"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Add"]</button>
            }else if (vfd.ButtonCode == "btnEdit")
            {
                <button class="btn btn-default" id="@btnId" v-bind:disabled="!selectedData" v-on:click="edit"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Update"]</button>
            }else if (vfd.ButtonCode == "btnDel")
            {
                <button class="btn btn-default" id="@btnId" v-bind:disabled="!selectedData" v-on:click="del"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Delete"]</button>
            }else if (vfd.ButtonCode == "btnEnable" || vfd.ButtonCode == "btnDisable")
            {
            }
            else
            {
                <button class="btn btn-default" id="@btnId"><i class="@vfd.ButtonIcon"></i>@vfd.ButtonName</button>
            }

        }

    }
    else
    {
        var roleFuncs = currentUser.RoleFuncs as List<RoleFunctionDto>;
        foreach (var vfd in viewFuncDtos)
        {
            //角色功能
            var rfbs = roleFuncs.Where(c => c.ObjectId == vfd.FeatureId);//新增，编辑是同一个功能
            if (rfbs.Any())
            {
                var btnId = controllerName + "_" + actionName + "_";
                btnId += vfd.ButtonCode;

                if (vfd.ButtonCode == "btnAdd")
                {
                    <button class="btn btn-default" id="@btnId" v-on:click="add"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Add"]</button>
                }else if (vfd.ButtonCode == "btnEdit")
                {
                    <button class="btn btn-default" id="@btnId" v-bind:disabled="!selectedData" v-on:click="edit"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Update"]</button>
                }else if (vfd.ButtonCode == "btnDel")
                {
                    <button class="btn btn-default" id="@btnId" v-bind:disabled="!selectedData" v-on:click="del"><i class="@vfd.ButtonIcon"></i>@SharedLocalizer["Delete"]</button>
                }else if (vfd.ButtonCode == "btnEnable" || vfd.ButtonCode == "btnDisable")
                {
                }
                else
                {
                    <button class="btn btn-default" id="@btnId" ><i class="@vfd.ButtonIcon"></i>@vfd.ButtonName</button>
                }

            }


        }

    }
}